package com.ccl.keyassistant.adapter;
/*
 *项目名     keyassistant
 *包名       com.ccl.keyassistant.adapter
 *文件名     KeyRecyclerAdapter
 *创建者     ccl
 *创建时间   2019-03-09 13:10
 *注解       KeyFragment的布局适配器
 */

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.ccl.keyassistant.R;
import com.ccl.keyassistant.entity.KeyItem;

import java.util.List;

public class KeyRecyclerAdapter extends RecyclerView.Adapter<KeyRecyclerAdapter.MyViewHolder> {
    private List<KeyItem> mDatas;
    private Context mContext;
    private OnItemClickListener onItemClickListener;

    public KeyRecyclerAdapter(Context context, List<KeyItem> mDatas) {
        mContext = context;
        this.mDatas = mDatas;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.key_recycler_item, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    //数据初始化
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
        KeyItem key = mDatas.get(position);
        holder.mAccountOrFileName.setText(key.getKeyname());
        holder.mKeyRemark.setText(key.getRemark());
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.itemClick(position);
                }
            });
        }

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void itemClick(int poistion);
    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        //账户或者文件名称
        private TextView mAccountOrFileName;
        //密码备注
        private TextView mKeyRemark;
        private ImageButton mInfo;

        public MyViewHolder(View itemView) {
            super(itemView);

            mAccountOrFileName = (TextView) itemView.findViewById(R.id.key_file_name);
            mKeyRemark = (TextView) itemView.findViewById(R.id.key_remark);
            mInfo = (ImageButton) itemView.findViewById(R.id.key_info);

        }
    }
}
